﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		EmailTemplate.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 // 
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using System.Collections.Generic;
using Dexter.Logger.Contracts;
using Dexter.Services;

namespace Dexter.Services.Implementation {
	/// <summary>
	/// 	Implementation of <see cref = "IEmailTemplate" />.
	/// </summary>
	public class EmailTemplate : Template , IEmailTemplate {
		const string EMAIL_SUBJECT = "DEXTER-TEMPLATE-SUBJECT";
		const string EMAIL_BODY = "DEXTER-TEMPLATE-BODY";

		/// <summary>
		/// Initializes a new instance of the <see cref="EmailTemplate"/> class.
		/// </summary>
		/// <param name="templateName">Name of the template.</param>
		/// <param name="blogTheme">The blog theme.</param>
		/// <param name="logger">The logger.</param>
		/// <param name="configurationService">The configuration service.</param>
		/// <param name="urlBuilderBase">The URL builder base.</param>
		/// <param name="themeManager">The theme manager.</param>
		public EmailTemplate ( string templateName , IBlogTheme blogTheme , ILogger logger , IConfigurationService configurationService , IUrlBuilderBase urlBuilderBase , IThemeManager themeManager ) : base ( templateName , blogTheme , logger , configurationService , urlBuilderBase , themeManager ) {}

		/// <summary>
		/// 	Gets the raw ubject of the email. It includes all markers like.
		/// 	<example>
		/// 		[SiteName]
		/// 	</example>
		/// </summary>
		/// <value>The subject.</value>
		public virtual string RawSubject {
			get { return base.ExtractSection ( EMAIL_SUBJECT ); }
		}

		/// <summary>
		/// 	Gets the raw body of the email. It includes all markers like.
		/// 	<example>
		/// 		[SiteName]
		/// 	</example>
		/// </summary>
		/// <value>The body.</value>
		public virtual string RawBody {
			get {
				return base.MergeWithMaster ( EMAIL_BODY );
			}
		}

		/// <summary>
		/// 	Format the <c>subject</c> of the email.
		/// </summary>
		/// <returns>The formatted subject.</returns>
		public virtual string GetFormatedSubject ( IDictionary<string, string> values ) {
			string result = ReplaceValues ( base.DefaultValues , RawSubject );

			return ReplaceValues ( values , result );
		}

		/// <summary>
		/// 	Format the <c>body</c> of the email.
		/// </summary>
		/// <returns>The formatted body.</returns>
		public virtual string GetFormatedBody ( IDictionary<string, string> values ) {
			string result = ReplaceValues ( base.DefaultValues , RawBody );

			return ReplaceValues ( values , result );
		}

		static string ReplaceValues ( IEnumerable <KeyValuePair <string , string>> values , string result ) {
			foreach ( var value in values ) {
				string oldValue = string.Format ( "${0}$" , value.Key );
				result = result.Replace ( oldValue , value.Value );
			}
			return result;
		}
	}
}
